package com.property.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.property.biz.CommunityBiz;
import com.property.biz.ComplaintBiz;
import com.property.biz.DisposeBiz;
import com.property.biz.InformBiz;
import com.property.biz.InformTargetBiz;
import com.property.biz.ManagementBiz;
import com.property.biz.OwnerBiz;
import com.property.biz.UserBiz;
import com.property.entity.Complaint;
import com.property.entity.Inform;
import com.property.entity.InformTarget;
import com.property.entity.Management;
import com.property.entity.Owner;
import com.property.entity.User;

@Controller
@RequestMapping("/main")
public class MainController {

	@Autowired
	private OwnerBiz ownerBiz;
	@Autowired
	private ManagementBiz managementBiz;
	@Autowired
	private UserBiz userBiz;
	@Autowired 
	private CommunityBiz communityBiz;
	@Autowired
	private ComplaintBiz complaintBiz;
	
	@Autowired
	private InformBiz informBiz;
	
	@Autowired
	private DisposeBiz disposeBiz;
	
	@Autowired
	private InformTargetBiz informTargetBiz;
	
	@RequestMapping("/index")
	public String index() {
		return "main/index";
	}

	@RequestMapping("/houselist")
	public String houselist(Model model, HttpSession session, String key,Integer page) {
		System.out.println(key);
		User users = (User) session.getAttribute("user");
		int pageSize =3;
		page=page==null?1:page;
		
		int startIndex=(page-1)*pageSize;
		Management man = managementBiz.ManGet(users.getId());
		System.out.println(man.getId());
		
		List<Owner> list = ownerBiz.getKey(man.getCommunity().getId(), key, key, key, startIndex, pageSize);
		int row=ownerBiz.rowCount(man.getCommunity().getId(), key, key, key);
		int pageCount=(int) Math.ceil((double)row/pageSize);
		model.addAttribute("page",page);
		model.addAttribute("houselist", list);
		model.addAttribute("totalPages", pageCount);
		System.out.println("aa"+pageCount);
		System.out.println("bb"+page);
		System.out.println(list.size());
		return "main/houselist";
	}
	
	@RequestMapping("/ownerdetail")
	@ResponseBody
	public Owner  ownerdetail(int id){
		Owner owner=ownerBiz.getbyId(id);
		return owner;
	}
	
	@RequestMapping("/complaa")	
	public String compla(Model model,HttpSession session,Integer page) {		
			User user=(User) session.getAttribute("user");
			page = page == null ? 1 : page;
			int pageSize = 5;
			int startIndex = (page - 1) * pageSize;
			Management management=managementBiz.ManGet(user.getId());
			int rows = complaintBiz.getTSCount(management.getCommunity().getId());
			int pageCount = (int) Math.ceil((double) rows / pageSize);
			String type="投诉";		
	          if(management!=null){      
	        	  List<Complaint>  complaint=complaintBiz.getAll(startIndex,pageSize,type,management.getCommunity().getId());
	        	  System.out.println(management.getCommunity().getId());
		        for (Complaint complaint1 : complaint) {	
					    model.addAttribute("complaint", complaint);
						model.addAttribute("totalPages", pageCount);
					    model.addAttribute("page", page);	
				   }	  	  
	          }
		return "main/complaa";
	}	
	
	@RequestMapping("/reportdetail")
	public String detail(Model model,Integer id){
		id=id==null?1:id;
		Complaint complaint = complaintBiz.ComAll(id);
		complaintBiz.updateCom(id);
		model.addAttribute("detail", complaint);
		return "main/reportdetail";
	}
	
	
	@RequestMapping("/compilewrite")
	public String compilewritea(Model model,HttpSession session,String userName,String password) {	
		
		return "main/compilewrite";
	}
	
	@RequestMapping(value="/docompilewrite",method=RequestMethod.POST)
	public String compilewrite(Model model,HttpSession session,String name,String password,String email) {	
		User user=(User) session.getAttribute("user");
		   user.setUserName(name);
		   user.setEmail(email);
		   user.setPassWord(password);
		if(user.getUserName()==name ){
	     userBiz.updateUser(user);
		}
		else{
			return  "main/compilewrite";
		}
		return "main/index";
	}
	@RequestMapping("/inform")//通知
	public String inform(Model model, HttpSession session, Inform inform,
			String type, String content) {
		User users = (User) session.getAttribute("user");
		Management man = managementBiz.ManGet(users.getId());
		System.out.println(man.getId());
		List<Owner> lists = ownerBiz.getListId(man.getId());
		model.addAttribute("list", lists);
		return "main/inform";

	}

	@RequestMapping("/doform")
	public String doinform(Inform inform, String type, String content,
			Model model, Management management, HttpSession session) {
		User users = (User) session.getAttribute("user");
		Management man = managementBiz.ManGet(users.getId());
		System.out.println(man.getId());
		inform.setType(type);
		management.setId(man.getId());
		inform.setContent(content);
		inform.setManagement(management);
		informBiz.addInform(inform);
		return "main/inform";
	}

	@RequestMapping("/getOwner")//得到修改业主信息的id
	@ResponseBody
	public Owner getOwner( int id) {
		Owner owner = ownerBiz.getbyId(id);
		
		System.out.println(owner.getId()+"///"+owner.getName());
		return owner;
	}

	@RequestMapping("informlist")//通知列表
	public String informlist(Inform inform ,Model model,HttpSession session,Integer page) {
		User users = (User) session.getAttribute("user");
		Management man = managementBiz.ManGet(users.getId());
		int pageSize=8;
		page=page==null?1:page;
		int startIndex=(page-1)*pageSize;
		List<Inform> informlist=informBiz.getAll(man.getId(), pageSize, startIndex);
		int row=informBiz.getRowCount(man.getId());
		int pagecount=(int) Math.ceil((double)row/pageSize);
		model.addAttribute("ilist",informlist);
		model.addAttribute("page",page);
		model.addAttribute("totalPages",pagecount);
		System.out.println("ascscsz"+row);
		System.out.println(man.getId());
		return "main/informlist";

	}
	
	@RequestMapping("/updateOwner")//修改业主信息
	public String updateOwner(Owner owner){
		ownerBiz.updateOwner(owner);
		return "redirect:houselist";
	}
	@RequestMapping("/deteleOwner")
	public String deteleOwner(int id){
		
		List<Complaint> list=complaintBiz.getComplaints(id);
		List<Inform> list2=informBiz.getInforms(id);
		for(Complaint complaint:list){
			disposeBiz.deleteDispose(complaint.getId());
		}
		complaintBiz.deleteComp(id);
		for(Inform inform :list2){
			informBiz.deleteInform(inform.getId());
		}
		informTargetBiz.deleteinTarget(id);
		ownerBiz.deleteOwner(id);
		return "redirect:houselist";
	}
	
	@RequestMapping("/getInformId")
	public String getInformId(Model model,int id,Integer page){
		int pageSize=3;
		page=page==null?1:page;
		
		int startIndex=(page-1)*pageSize;
	List<InformTarget> informTarget=informTargetBiz.getinformId(id,startIndex , pageSize);
		int rows=informTargetBiz.RowCount((int)id);
		int pageCount=(int) Math.ceil((double)rows/pageSize);
		
		model.addAttribute("list",informTarget);
		model.addAttribute("page",page);
		model.addAttribute("totalPages", pageCount);
		System.out.println(" 大小"+informTarget.size());
		System.out.println("1"+page);
		System.out.println("2"+pageCount);
		return "main/inflistdetail";	
		
	}
}
